package com.yishun.springboot.util;

public class PageHelper {
	private int totalItems; // 总条目数
	private int pageSize; // 每页显示条目数
	private int currentPage; // 当前页码

	// 总页数
	public PageHelper(int totalItems, int pageSize, int currentPage) {
		this.totalItems = totalItems;
		this.pageSize = pageSize;
		this.currentPage = currentPage;
		if (!hasNextPage()) {
			this.currentPage = getTotalPages();
		}
		if (!hasPreviousPage()) {
			this.currentPage = 1;
		}
	}

	public int getTotalPages() {
		return (int) Math.ceil((double) totalItems / pageSize);
	}

	// 偏移量
	public int getOffset() {
		return (currentPage - 1) * pageSize;
	}

	// 限制条目数
	public int getLimit() {
		return pageSize;
	}

	// 当前页码
	public int getCurrentPage() {
		return currentPage;
	}

	// 是否有下一页
	public boolean hasNextPage() {
		return currentPage < getTotalPages();
	}

	// 是否有上一页
	public boolean hasPreviousPage() {
		return currentPage > 1;
	}
}
